package com.plugin.sproutcore.core.launch;

import java.util.Map;

import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.IProcessFactory;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.RuntimeProcess;

import com.plugin.sproutcore.util.SystemUtil;

/**
 * A custom process factory for running SproutCore's server. It is designed to
 * handle different execution environments.
 */
public class SproutCoreProcessFactory implements IProcessFactory 
{
	public static final String PROCESS_FACTORY_ID = "com.plugin.SproutCore.processFactory";

	/**
	 * Will create a windows specific process 
	 * (<code>SproutCoreProcessFactory</code>) that will terminate the 
	 * SproutCore server process successfully. Windows does not terminate child 
	 * processes that are still running. I use this process to get the PID of 
	 * the child process and use the id to destroy the running process. 
	 * 
	 * If the system environment is a unix machine then the process will return 
	 * an instance of <code>org.eclipse.debug.core.model.RuntimeProcess</code>.
	 *
	 * @param launch the launch the process is contained in
	 * @param process the system process to wrap
	 * @param label the label assigned to the process
	 * @param attributes initial values for the attribute map
	 * @return the process
	 * @see IProcessFactory
	 * @see SproutCoreWinRuntimeProcess
	 */
	@SuppressWarnings("unchecked")
	public IProcess newProcess(ILaunch launch, Process process, String label,
			Map attributes) 
	{
		int os = SystemUtil.os();
		if (os == SystemUtil.WINDOWS) 
		{
			return new SproutCoreWinRuntimeProcess(launch, process, label, attributes);
		}
		// Use the default runtime process
		return new RuntimeProcess(launch, process, label, attributes);
	}

}
